<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Banner;
use app\common\model\BasePay;
use app\common\model\Brach;
use app\common\model\Brand;
use app\common\model\Notice;
use app\common\model\Order;
use app\common\model\Page;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $bannerList = Banner::where('status' , 1)->order('sort desc')->select() ;
        $noticeList = Notice::where('status' , 1)->order('id desc')->select() ;
        $this->success('请求成功' , [
            'banner' => $bannerList ,
            'noticeList' => $noticeList ,
        ]);
    }
    public function top_car()
    {
        $page = $this->request->post('page' , 1 , 'intval') ;
        $list = Order::where('top_status' , Order::TOP_STATUS_TOP)
            ->field('id,price,title,img_url,type')
            ->where('check_status' , Order::CHECK_STATUS_PASS)
            ->order('id desc')->page($page , 5)->select() ;
        $this->success('请求成功' ,$list);
    }
    public function brand()
    {
        $list = Brand::where('status' , 1)
            ->field('id,title')
            ->order('sort desc')->select() ;
        $this->success('请求成功' ,$list);
    }
    public function car_list()
    {
        $page = $this->request->post('page' , 1 , 'intval') ;
        $brandId = $this->request->post('brand_id' , 0 , 'intval') ;
        $where['check_status'] = Order::CHECK_STATUS_PASS ;
        if($brandId > 0){
            $where['brand_id'] = $brandId;
        }
        $list = Order::where($where)->field('id,price,title,img_url,type')
            ->order('id desc')->page($page , 10)->select() ;
        $this->success('请求成功' ,$list);
    }
    public function car_info()
    {
        $id = $this->request->post('id' , 0 , 'intval') ;

        $info = Order::where('id' , $id)->find() ;
        if($info == []){
            $this->error('暂无数据');
        }
        $info->img_urls = explode(',' , $info['img_urls'] ) ;
        $info->brand_name = $info->brand->title ;
        $this->success('请求成功' ,[
            'info' => $info ,
            'user' => [
                'username' => $info->user->username ,
                'img_url' => $info->user->img_url ,
            ] ,
            'tel' => 15613196281
        ]);
    }
    public function user_info()
    {
        $id = $this->request->post('id' , 0 , 'intval') ;
        $user = \app\common\model\User::get($id) ;

        $this->success('请求成功' ,[
            'user' => [
                'username' => $user->username ,
                'img_url' => $user->img_url ,
            ] ,
            'total' => [
                'num' => 1,
                'num2' => 10,
                'num3' => 20,
            ]
        ]);
    }
    public function user_car_list()
    {
        $id = $this->request->post('id' , 0 , 'intval') ;
        $type = $this->request->post('type' , 1 , 'intval') ;
        $page = $this->request->post('page' , 1 , 'intval') ;
        $where['user_id'] = $id ;
        $where['type'] = $type ;
        $where['check_status'] = Order::CHECK_STATUS_PASS ;
        $list = Order::where($where)->field('id,price,title,img_url,type')
            ->order('id desc')->page($page , 10)->select() ;
        $this->success('请求成功' , [
            'list' => $list ,
        ]) ;
    }
    public function payment()
    {
        $list = BasePay::where('status' , 1)->select() ;
        $this->success('请求成功' , [
            'list' => $list ,
        ]) ;
    }
    public function upload()
    {
        $file = $this->request->file('file');
        try {
            $upload = new Upload($file);
            $attachment = $upload->upload();
        } catch (UploadException $e) {
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
    }
    public function page()
    {
        $id = $this->request->post('type' ,0) ;
        $info = Page::where('id' , $id)->find() ;
        $this->success('请求成功' ,$info) ;
    }
}
